
package org.upt.dcda.presentation;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.upt.dcda.NotificationManagerBeanRemote;
import org.upt.dcda.db.UserEntity;
import org.upt.dcda.exceptions.BookstoreException;

/**
 * Servlet implementation class NotificationCreateServlet
 */
@WebServlet("/NotificationCreateServlet")
public class NotificationCreateServlet extends HttpServlet
{

	private static final long				serialVersionUID	= 1L;

	@EJB
	private NotificationManagerBeanRemote	notificationManagerBean;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NotificationCreateServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		try
		{
			if (SessionAttributes.isLoggedIn(request))
			{
				int bookId = Integer.valueOf(request.getParameter("bookId"));
				int userId = ((UserEntity) request.getSession().getAttribute(SessionAttributes.USER)).getUserId();
				this.notificationManagerBean.addNotification(userId, bookId);
				response.sendRedirect(request.getContextPath() + "/displaybook.jsp?id=" + bookId);
			}
			else
			{
				response.sendRedirect(request.getContextPath() + "login.jsp");
			}
		}
		catch (BookstoreException be)
		{
			getServletContext().log("Caught BookstoreException: " + be, be);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, be.toString());
		}
	}

}
